//
//  ShopMainViewController.m
//  1243
//
//  Created by 王 on 16/5/26.
//  Copyright © 2016年 1243. All rights reserved.
//

#import "ShopMainViewController.h"
#import "ShoperDetailViewController.h"
#import "DetailViewController.h"
#import "CommentViewController.h"
#import "ShopCartViewController.h"
#import "QueRenDingDanViewController.h"
#import "MBProgressHUD.h"
#import "ShopOneCollectionViewController.h"

@interface ShopMainViewController ()

@property (nonatomic, strong) UISegmentedControl *segment;

@property (nonatomic, strong) DetailViewController *detailVC;
@property (nonatomic, strong) CommentViewController *commentVC;
@property (nonatomic, strong) ShoperDetailViewController *shopDetailVC;

@end

@implementation ShopMainViewController

- (ShoperDetailViewController *)shopDetailVC {
    if (!_shopDetailVC) {
        _shopDetailVC = [[ShoperDetailViewController alloc] init];
        
        _shopDetailVC.xingYingShopID = self.xingYingShopID;
        _shopDetailVC.str = self.str;
    }
    return _shopDetailVC;
}

- (DetailViewController *)detailVC {
    if (!_detailVC) {
        _detailVC = [[DetailViewController alloc] init];
        _detailVC.shopId = self.xingYingShopID;
    }
    return _detailVC;
}

- (CommentViewController *)commentVC {
    if (!_commentVC) {
        _commentVC = [[CommentViewController alloc] init];
    }
    return _commentVC;
}

- (UISegmentedControl *)segment {
    if (!_segment) {
        _segment = [[UISegmentedControl alloc] initWithItems:@[@"商品",@"详情",@"评论"]];
        _segment.width = 150;
        _segment.tintColor = [UIColor clearColor];//去掉颜色,现在整个segment都看不见
        NSDictionary* selectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:16], NSForegroundColorAttributeName: [UIColor whiteColor]};
        [_segment setTitleTextAttributes:selectedTextAttributes forState:UIControlStateSelected];//设置文字属性
        NSDictionary* unselectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:16], NSForegroundColorAttributeName: [UIColor lightTextColor]};
        [_segment setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];
        //设置segment初始被选中的下标
        _segment.selectedSegmentIndex = 0;
    }
    return _segment;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    self.navigationItem.titleView = self.segment;
    self.segment.selectedSegmentIndex = 0;
    [self.segment addTarget:self action:@selector(segmentedControlValueChanged) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.shopDetailVC.view];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushShopCart) name:@"pushShopCart" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushQueRenVC:) name:@"pushQueRenVC" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(isCollect:) name:@"isCollect" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushShopOneCollectionVC:) name:@"pushShopOneCollectionVC" object:nil];
}

- (void)pushShopCart {
    
    ShopCartViewController *shopCartVC = [[ShopCartViewController alloc] init];
    [self.navigationController pushViewController:shopCartVC animated:YES];
}

- (void)pushQueRenVC:(NSNotification *)noti {
    
    QueRenDingDanViewController *qrVC = [[QueRenDingDanViewController alloc] init];
    qrVC.str = self.str;
    qrVC.num = 0;
    
    qrVC.dingDanArrM = [NSMutableArray array];
    [qrVC.dingDanArrM addObject:noti.userInfo[@"Dictionary"]];
    qrVC.shopDic = [NSDictionary dictionary];
    qrVC.shopDic = noti.userInfo[@"Dictionary"];
    [self.navigationController pushViewController:qrVC animated:YES];
}

- (void)isCollect:(NSNotification *)noti {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.label.text = NSLocalizedString(noti.userInfo[@"Message"], @"HUD message title");
    hud.offset = CGPointMake(0.f, 80.f);
    
    [hud hideAnimated:YES afterDelay:1.f];
}

- (void)pushShopOneCollectionVC:(NSNotification *)noti {
    ShopOneCollectionViewController *shopOneVC = [[ShopOneCollectionViewController alloc] init];
    shopOneVC.num = 1;
    shopOneVC.merchantId = noti.userInfo[@"MerchantId"];
    //    [self.navigationController pushViewController:shopOneVC animated:YES];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.label.text = NSLocalizedString(@"暂无此功能", @"HUD message title");
    hud.offset = CGPointMake(0.f, 80.f);
    
    [hud hideAnimated:YES afterDelay:1.f];
}

- (void)segmentedControlValueChanged {
    
    NSInteger index = self.segment.selectedSegmentIndex;
    if (index == 1) {
        [self.view addSubview:self.detailVC.view];
        [self.shopDetailVC.view removeFromSuperview];
        [self.commentVC.view removeFromSuperview];
    } else if (index == 2) {
        [self.view addSubview:self.commentVC.view];
        [self.detailVC.view removeFromSuperview];
        [self.shopDetailVC.view removeFromSuperview];
    } else {
        [self.view addSubview:self.shopDetailVC.view];
        [self.detailVC.view removeFromSuperview];
        [self.commentVC.view removeFromSuperview];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
